package com.docman.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.docman.dao.DocumentDAO;
import com.docman.domainmodel.DocumentBean;
import com.docman.domainmodel.UserBean;


@WebServlet("/ProfileServlet")
public class ProfileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public ProfileServlet() {
        super();
       
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();	    
        UserBean user = (UserBean)session.getAttribute("currentSessionUser");
		
        if(user != null){
        
		ArrayList<DocumentBean> docs = new ArrayList<DocumentBean>();
  		
			docs = DocumentDAO.getUserUploads(user.getUserName());
			System.out.println("LoginServlet user docs:"+docs+"\n");
			request.setAttribute("userDocList", docs);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/profile.jsp");

			  if (dispatcher != null){
				  //System.out.println(docs.toString());
			  dispatcher.forward(request, response);

			  }
        }
        else {
        	response.sendRedirect("profile.jsp");
        }
	}

}
